package com.leostadyn.stackfarm.timestatistics.view.activity.admin;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.model.bean.DetailProjectBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.EmployeeBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.StaffTimeRecordBean;
import com.leostadyn.stackfarm.timestatistics.presenter.admin.FetchListPresenter;
import com.leostadyn.stackfarm.timestatistics.utils.DateUtils;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;
import com.leostadyn.stackfarm.timestatistics.view.activity.MVPActivity;
import com.leostadyn.stackfarm.timestatistics.view.fragment.ProjectFragment;
import com.leostadyn.stackfarm.timestatistics.view.vinterface.admin.IFetchListView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class DetailPersonnelActivity extends MVPActivity<IFetchListView, FetchListPresenter> implements IFetchListView, ProjectFragment.ProjectCallBack, View.OnClickListener, ProjectFragment.SetProjectCallBack {
    ArrayList<DetailProjectBean> projectBeans = new ArrayList<>();
    ProgressBar progressBar;
    ProjectFragment projectFragment;
    TextView startTxt;
    TextView endTxt;
    TextView dayTxt;
    String uid;
    Button searchBtn;
    private Date start;
    private Date end;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_personnel_hours_detail);
        initView();
        mPresenter.toProject(end, start, uid, compositeDisposable);
    }

    void initView() {
        findViewById(R.id.nav_img_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.nav_btn_add).setVisibility(View.GONE);
        TextView textView = findViewById(R.id.nav_txt_title);
        findViewById(R.id.layout_start).setOnClickListener(this);
        findViewById(R.id.layout_end).setOnClickListener(this);
        startTxt = findViewById(R.id.txt_start);
        endTxt = findViewById(R.id.txt_end);
        progressBar = findViewById(R.id.progressBar3);
        textView.setText(getResources().getString(R.string.details_of_staff_hours));
        searchBtn = findViewById(R.id.btn_search);
        searchBtn.setOnClickListener(this);

        initText();

        Intent intent = getIntent();
        TextView nameTxt = findViewById(R.id.txt_personnel_name);
        TextView jobTxt = findViewById(R.id.txt_personnel_number);
        dayTxt = findViewById(R.id.txt_personnel_day);
        uid = intent.getStringExtra("uid");
        nameTxt.setText(intent.getStringExtra("name"));
        jobTxt.setText(intent.getStringExtra("job"));
        dayTxt.setText("总工时： 0.0 天");
    }

    void initFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (projectFragment == null) {
            projectFragment = ProjectFragment.newInstance(projectBeans);
            fragmentTransaction.add(R.id.frame_layout, projectFragment).commitAllowingStateLoss();
        } else {
            fragmentTransaction.remove(projectFragment);
            projectFragment = ProjectFragment.newInstance(projectBeans);
            fragmentTransaction.add(R.id.frame_layout, projectFragment).commitAllowingStateLoss();
        }
    }

    void initText() {
        start = DateUtils.getDayOfWeek(Calendar.MONDAY, -1);
        end = DateUtils.getDayOfWeek(Calendar.SUNDAY, -1);

        startTxt.setText(DateUtils.getStringDate(start));
        endTxt.setText(DateUtils.getStringDate(end));
    }

    @Override
    protected FetchListPresenter createPresenter() {
        return new FetchListPresenter();
    }

    @Override
    public void successProjectMessage(ArrayList<DetailProjectBean> projectList) {
        projectBeans = projectList;
        initFragment();
    }

    @Override
    public void successEmployeeMessage(ArrayList<EmployeeBean> employeeList) {

    }

    @Override
    public void successMessage(ArrayList<DetailProjectBean> projectList, ArrayList<EmployeeBean> employeeList) {

    }

    @Override
    public void failureMessage(String s) {
        ToastUtil.show(this, s);
    }

    @Override
    public void successTimeRecord(StaffTimeRecordBean staffTimeRecordBean) {

    }

    @Override
    public void showWaiting() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismissWaiting() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void disconnected() {

    }

    @Override
    public void sentProjectMessage() {
        mPresenter.toProject(end, start, uid, compositeDisposable);
    }

    @Override
    public void setPersonnelList(ArrayList<DetailProjectBean> arrayList) {
        Collections.sort(arrayList, Collections.reverseOrder());
    }

    @Override
    public String setProjectTitle() {
        return "工时";
    }

    @Override
    public void setPersonnelWorkingHours(double d) {
        dayTxt.setText("总工时： " + d + " 天");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_start:
                Calendar calendarstart = Calendar.getInstance();
                DatePickerDialog dialogstart = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        String str = String.format("%02d-%02d", month + 1, dayOfMonth);
                        startTxt.setText(str);
                        String d = String.format("%04d-%02d-%02d", year, month + 1, dayOfMonth);
                        start = DateUtils.getDate(d);
                    }
                },
                        calendarstart.get(Calendar.YEAR),
                        calendarstart.get(Calendar.MONTH),
                        calendarstart.get(Calendar.DAY_OF_MONTH));
                dialogstart.show();
                break;

            case R.id.layout_end:
                Calendar calendarend = Calendar.getInstance();
                DatePickerDialog dialogend = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        String str = String.format("%02d-%02d", month + 1, dayOfMonth);
                        String d = String.format("%04d-%02d-%02d", year, month + 1, dayOfMonth);
                        end = DateUtils.getDate(d);
                        endTxt.setText(str);
                    }
                },
                        calendarend.get(Calendar.YEAR),
                        calendarend.get(Calendar.MONTH),
                        calendarend.get(Calendar.DAY_OF_MONTH));
                dialogend.show();
                break;

            case R.id.btn_search:
                mPresenter.toProject(end, start, uid, compositeDisposable);
                break;

            default:
                break;
        }
    }


    @Override
    public void mainProjectClick(int position, List<DetailProjectBean> dataBeanList) {

    }
}
